package com.frx.sort;


import java.util.Arrays;

/**
 * @description 冒泡排序
 * Created by xurenfeng on 2019/2/18.
 */
public class BubleSort<T extends Comparable<T>> extends Sort<T>{

    @Override
    public void sort(T[] nums) {
        int N = nums.length;
        boolean hasSorted = false;
        for (int i = N-1;i > 0 && !hasSorted;i--){
            hasSorted = true;
            for (int j = 0;j < i;j++){
                if (less(nums[j+1],nums[j])){
                    hasSorted = false;
                    swap(nums,j,j+1);
                }
            }
        }
    }

    public static void main(String[] args){
        Integer[] nums = {2,1,5,3,7,8};
        new BubleSort<Integer>().sort(nums);
        System.out.println(Arrays.toString(nums));
    }
}
